#include "goalwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QMessageBox>

GoalWindow::GoalWindow(double currentBmi, QWidget *parent) : QMainWindow(parent), currentBmi(currentBmi)
{
    setWindowTitle("设定理想BMI目标");
    resize(500, 400);

    QLabel *titleLabel = new QLabel("设定您的BMI目标", this);
    titleLabel->setStyleSheet("font-size: 16pt; font-weight: bold;");

    QLabel *targetLabel = new QLabel("目标BMI:", this);
    targetBmiInput = new QLineEdit(this);
    targetBmiInput->setPlaceholderText("请输入目标BMI值 (18.5-24为健康范围)");

    QLabel *timeframeLabel = new QLabel("期望实现期限(月):", this);
    timeframeInput = new QLineEdit(this);
    timeframeInput->setPlaceholderText("请输入期望几个月内达成");

    evaluateBtn = new QPushButton("评估可行性", this);
    evaluateBtn->setStyleSheet("QPushButton { padding: 8px; font-size: 12pt; }");

    adviceText = new QTextEdit(this);
    adviceText->setReadOnly(true);

    connect(evaluateBtn, &QPushButton::clicked, [this, currentBmi]() {
        bool ok;
        double targetBmi = targetBmiInput->text().toDouble(&ok);
        if (!ok || targetBmi < 15 || targetBmi > 40) {
            QMessageBox::warning(this, "错误", "请输入有效的BMI值(15-40)");
            return;
        }

        int months = timeframeInput->text().toInt(&ok);
        if (!ok || months <= 0) {
            QMessageBox::warning(this, "错误", "请输入有效的月数");
            return;
        }

        double bmiChange = targetBmi - currentBmi;
        double monthlyChange = bmiChange / months;
        
        QString advice;
        if (fabs(monthlyChange) > 0.5) {
            advice = QString("您的目标过于激进(每月变化%1)。当前BMI(%2)与目标BMI(%3)的差值为%4。建议:\n")
                .arg(monthlyChange, 0, 'f', 2)
                .arg(currentBmi, 0, 'f', 1)
                .arg(targetBmi, 0, 'f', 1)
                .arg(bmiChange, 0, 'f', 1);
            advice += "- 延长实现期限\n";
            advice += "- 咨询专业医生\n";
        } else {
            advice = QString("您的目标可行(每月变化%1)。当前BMI(%2)与目标BMI(%3)的差值为%4。建议:\n")
                .arg(monthlyChange, 0, 'f', 2)
                .arg(currentBmi, 0, 'f', 1)
                .arg(targetBmi, 0, 'f', 1)
                .arg(bmiChange, 0, 'f', 1);
            if (bmiChange > 0) {
                advice += "- 需要增重\n";
                advice += "- 增加蛋白质摄入\n";
                advice += "- 进行力量训练\n";
            } else if (bmiChange < 0) {
                advice += "- 需要减重\n";
                advice += "- 控制饮食热量\n";
                advice += "- 增加有氧运动\n";
            } else {
                advice += "- 当前BMI已达到目标\n";
            }
            advice += "- 每月监测BMI变化\n";
        }

        adviceText->setText(advice);
    });

    backButton = new QPushButton("返回", this);
    backButton->setStyleSheet(
        "QPushButton {"
        "  padding: 8px 16px;"
        "  font-size: 12pt;"
        "  background-color: #6c757d;"
        "  color: white;"
        "  border-radius: 4px;"
        "}"
    );

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(evaluateBtn);

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(titleLabel);
    layout->addWidget(targetLabel);
    layout->addWidget(targetBmiInput);
    layout->addWidget(timeframeLabel);
    layout->addWidget(timeframeInput);
    layout->addLayout(buttonLayout);
    layout->addWidget(adviceText);

    connect(backButton, &QPushButton::clicked, this, &GoalWindow::returnToPrevious);

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
}